package com.infore.airsupervise.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.bean.PolluterLicenseE;
import com.infore.airsupervise.biz.PolluterLicenseBiz;
import com.infore.airsupervise.biz.impl.PolluterLicenseBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.ui.widget.SwipeRecyclerView;
import com.infore.airsupervise.utils.JumpUtil;
import com.infore.airsupervise.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 排污许可证列表
 *
 * @作者: dingxinlin
 * @创建时间: 2017/4/14 10:06
 */
public class LicenseListActivity
        extends BaseTitleBarActivity
        implements SwipeRecyclerView.OnActionListener {

    @BindView(R.id.refreshRecyclerView)
    SwipeRecyclerView swipeRecyclerView;

    private String mnStr;

    private PolluterLicenseBiz polluterLicenseBiz;

    private BaseQuickAdapter<PolluterLicenseE, BaseViewHolder> quickAdapter;
    private List<PolluterLicenseE> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_license_list);
        ButterKnife.bind(this);

        mnStr = getIntent().getStringExtra(Constants.INTENT_STR_KEY);
        polluterLicenseBiz = new PolluterLicenseBizImpl();

        initView();
    }

    private void initView() {
        title.setText("排污许可证列表");
        rightMenu.setVisibility(View.GONE);

        if (mnStr == null || mnStr.isEmpty()) {
            ToastUtil.showLong(ctx, "Intent参数传递失败");
            return;
        }

        // 适配器
        quickAdapter = new BaseQuickAdapter<PolluterLicenseE, BaseViewHolder>(R.layout.item_license_list, datas) {
            @Override
            protected void convert(BaseViewHolder helper, final PolluterLicenseE item) {
                helper.setText(R.id.company_name, item.getCompanyName());
                helper.setText(R.id.license_code, "许可证编号：" + item.getNumber());
                helper.setText(R.id.license_content, "许可内容：" + item.getContent());
            }
        };

        // 设置recyclerView
        swipeRecyclerView.setAdapter(quickAdapter);
        swipeRecyclerView.setActionListener(this);

        // 首次自动刷新
        swipeRecyclerView.getSwipeLayout().setRefreshing(true);
        swipeRecyclerView.onRefresh();
    }

    @Override
    public void onRefresh() {
        getLicenseDatas(true);
    }

    @Override
    public void onLoadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRecyclerView.getAdapter().loadMoreEnd();
                swipeRecyclerView.getSwipeLayout().setEnabled(true);
            }
        }, 1000);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        PolluterLicenseE item = datas.get(position);
        JumpUtil.jumpFromTo(ctx, LicenseDetailActivity.class, Constants.INTENT_OBJ_KEY, item);
    }

    /**
     * 发起请求
     */
    private void getLicenseDatas(final boolean isRefresh) {
        polluterLicenseBiz.getPolluterLicenseByMN(mnStr,
                new OnServerResponseListener<List<PolluterLicenseE>>() {
                    @Override
                    public void onSucceed(List<PolluterLicenseE> result, String desc) {
                        swipeRecyclerView.commonSuccessHandle(isRefresh, result.size());

                        datas.clear();
                        datas.addAll(result);
                        swipeRecyclerView.getAdapter().notifyDataSetChanged();
                    }

                    @Override
                    public void onFailed(int errorCode, String title, String desc) {
                        swipeRecyclerView.commonFailedHandle(isRefresh, errorCode, title, desc);
                    }
                });
    }
}
